# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:inverse_function2_0.py
@Time:2024/8/2 9:45

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""

import numpy as np
import matplotlib.pyplot as plt

# 参数a和b
a = 1
b = 2

# x的范围
x = np.linspace(-5, 5, 400)

# 原始函数y = ax^2 + b
y = a * x**2 + b

# 反函数（只考虑正根）
# 注意：我们只考虑y >= b的情况
y_inv_domain = np.linspace(b, 30, 400)  # 反函数y的取值范围
x_inv = np.sqrt((y_inv_domain - b) / a)

# 绘制原始函数
plt.plot(x, y, label=f'y = {a}x^2 + {b}')

# 绘制反函数
plt.plot(x_inv, y_inv_domain, label='Inverse Function (Positive Root)', color='red')

# 添加图例
plt.legend()

# 设置标题和坐标轴标签
plt.title('Function and Its Inverse')
plt.xlabel('x')
plt.ylabel('y')

plt.grid(True)
plt.show()